/**
 * 网店管理 vuex模块
 * @type {{mutations: {}, state: (function(): {}), actions: {addItem({state: *, commit: *, rootState: *}), modifyItem({state: *, commit: *, rootState: *}), deleteItem({state: *, commit: *, rootState: *}), getItemAll({state: *, commit: *, rootState: *}), onItem({state: *, commit: *, rootState: *}), getItem({state: *, commit: *, rootState: *}), modifyNum({state: *, commit: *, rootState: *}), downItem({state: *, commit: *, rootState: *})}, namespaced: boolean}}
 */
import axios from "axios";
import Qs from "qs";

const manage={
  namespaced: true,
  state:()=>{
    return{
      store_info:{
        store_id:'',
        store_picture:'',
        store_name_cn:'',
        store_name_en:'',
        brand_name_cn:'',
        brand_name_en:'',
        brand_intro:'',
      },
      onSellList:[{
        item_id:'111',
        item_name:'奥迪',
        item_price:'999$',
        item_num:'234',
      }],
      offSellList:[{
        item_id:'222',
        item_name:'你妈逼',
        item_price:'999$',
      }],

    }
  },
  mutations:{
    loadNewPic(state,base){
      state.store_info.store_picture=base;
    }
  },
  actions:{
    //查询网店信息（每次进入时调用，看是否有网店）
    getStore({ state, commit, rootState }){
      return new Promise((resolve, reject) => {
        axios.get('/supply/store').then((res)=>{
          state.store_info=res.data;
          console.log(state.store_info);
          resolve();
        }).catch(e=>{
          reject();
        })
      })
    },

    //申请网店
    initStore({ state, commit, rootState },obj){
      return new Promise(((resolve, reject) => {
        axios.post('/supply/init',
          Qs.stringify(obj),
          {
            headers: {
              'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
            }
          }).then(()=>{
          resolve()
        }).catch(e=>{
          reject(e);
        })
      }))
    },

    //修改网点信息
    modifyStore({ state, commit, rootState },obj){
      return new Promise(((resolve, reject) => {
        axios.put('/supply/store',
          Qs.stringify(obj),
          {
            headers: {
              'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
            }
          }).then(()=>{
          resolve()
        }).catch(e=>{
          reject(e);
        })
      }))
    },

    //获取所有在售货物信息
    getOnSellItemAll({ state, commit, rootState }){
      return new Promise(((resolve, reject) => {
        axios.post('/supply/onsell/querylist',
          Qs.stringify({store_id:state.store_info.store_id}),
          {
            headers: {
              'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
            }
          }).then((res)=>{
            state.onSellList=res.data;
            console.log(state.onSellList);
            resolve()
        }).catch(e=>{
          reject(e);
        })
      }))
    },

    //获取所有历史货物信息
    getOffSellItemAll({ state, commit, rootState }){
      return new Promise(((resolve, reject) => {
        axios.post('/supply/offsell/querylist',
          Qs.stringify({store_id:state.store_info.store_id}),
          {
            headers: {
              'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
            }
          }).then((res)=>{
            state.offSellList=res.data;
          console.log('历史货物信息'+res.data);
          resolve()
        }).catch(e=>{
          reject(e);
        })
      }))
    },

    //根据id查询货物详情
    getItem({ state, commit, rootState },item_id){
      return new Promise(((resolve, reject) => {
        axios.post('/supply/query_single_good',
          Qs.stringify({item_id:item_id}),
          {
            headers: {
              'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
            }
          }).then((res)=>{
            //直接返回，详细信息不储存在vuex内
            resolve(res.data);
        }).catch(e=>{
          reject(e);
        })
      }))
    },

    //增加货物（在售货物）
    addItem({ state, commit, rootState },obj){
      return new Promise(((resolve, reject) => {
        console.log(obj);
        axios.put('/supply/onsell/addgoods',
          Qs.stringify(obj),
          {
            headers: {
              'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
            }
          }).then((res)=>{
          resolve();
        }).catch(e=>{
          reject(e);
        })
      }))
    },

    //下架货物（在售货物）
    downItem({ state, commit, rootState }, item_id){
      return new Promise(((resolve, reject) => {
        axios.put('/supply/onsell/off_shelves',
          Qs.stringify({
            store_id:state.store_info.store_id,
            item_id:item_id,
          }),
          {
            headers: {
              'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
            }
          }).then((res)=>{
          resolve();
        }).catch(e=>{
          reject(e);
        })
      }))
    },

    //修改货物信息（在售货物）
    modifyItem({ state, commit, rootState },obj){
      return new Promise(((resolve, reject) => {
        axios.put('/supply/onsell/modifygoods',
          Qs.stringify(obj),
          {
            headers: {
              'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
            }
          }).then((res)=>{
          resolve();
        }).catch(e=>{
          reject(e);
        })
      }))
    },

    //更改库存（在售货物）只有供货商有此功能
    modifyNum({ state, commit, rootState },obj){
      return new Promise(((resolve, reject) => {
        axios.put('/supply/onsell/changenum',
          Qs.stringify(obj),
          {
            headers: {
              'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
            }
          }).then((res)=>{
          resolve();
        }).catch(e=>{
          reject(e);
        })
      }))
    },

    //历史货物上架
    onItem({ state, commit, rootState },obj){
      ///supply/offsell/on_shelves
      return new Promise(((resolve, reject) => {
        axios.put('/supply/offsell/on_shelves',
          Qs.stringify(obj),
          {
            headers: {
              'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
            }
          }).then((res)=>{
          resolve();
        }).catch(e=>{
          reject(e);
        })
      }))
    },

    //删除货物（历史货物）
    deleteItem({ state, commit, rootState },item_id){
      return new Promise(((resolve, reject) => {
        axios.delete('/supply/offsell/delete?'+Qs.stringify({item_id:item_id})).then((res)=>{
          resolve();
        }).catch(e=>{
          reject(e);
        })
      }))
    },
  }
}

export default manage;
